package no.eirikb.bomberman.applet.game;

import java.io.Serializable;
import no.eirikb.bomberman.shared.User;

/**
 *
 * @author eirikb
 */
public class Bomb implements Serializable, Comparable<Bomb> {

    private int x;
    private int y;
    private int time;
    private int anim1;
    private int anim2;
    private int size;  // Ser serverside!
    private User user;  // Set serverside!

    public Bomb(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getSize() {
        return size;
    }

    public boolean explode() {
        return time-- == 0;
    }

    public int anim() {
        anim2++;
        if (anim2 == 5) {
            anim2 = 0;
            anim1++;
            if (anim1 > 2) {
                anim1 = 0;
            }
        }
        return anim1;
    }

    /**
     * Compare bomb to another bomb
     * -1: Bomb has no similarity
     * 0: Bomb has same x and y -coordiantes (Exactly the same bomb)
     * 1: Bomb is in blow-up-range of the exploding bomb (chainreaction)
     * @param bomb
     * @return
     */
    public int compareTo(Bomb bomb) {
        int i = x == bomb.getX() && y == bomb.getY() ? 0 : -1;
        if (i == 0) {
            return i;
        } else {
            System.out.println("chekk");
            if ((y == bomb.getY() && Math.abs(x - bomb.getX()) <= size) ||
                    (x == bomb.getX() && Math.abs(y - bomb.getY()) <= size)) {
                System.out.println("same");
                return 1;
            } else {
                System.out.println("nosatem");
                return -1;
            }
        }
    }
}
